import {request} from '../index'
import storage from '@system.storage'
import prompt from '@system.prompt'
export const getUserInfo = (isRefresh)=>{
    return new Promise(async(resolve,reject)=>{
        // 先从本地获取
        let userInfo = await storage.get({key:'user-detail'})
        if(userInfo?.data && !isRefresh) return resolve(JSON.parse(userInfo.data))
        // 服务器
        return request({
            url: '/user/account',
            method: 'post',
            data: {}
        }).then(res=>{
            storage.set({
                key:'user-detail',
                value:JSON.stringify(res)
            })
            resolve(res)
        })
        .catch(()=>{
            prompt.showToast({
                message: `好像出问题了，请稍后重试`
            })
            reject('好像出问题了，请稍后重试')
        })
    })
}